package cameraClassThread.uweTronnier.fhkl.de;

import cameraClassThread.uweTronnier.fhkl.de.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class CameraClassThreadActivity extends Activity implements OnTouchListener {
	
	OurView v;
	Bitmap icon;
	float x, y;
	float scale;
	TouchType touchState = TouchType.NONE;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        v = new OurView(this);
        v.setOnTouchListener(this);
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.droid);
        x = y = 0;
        scale = 1.0f;
        setContentView(v);       
    }
    
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		v.pause();
		super.onPause();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		v.resume();
		}
//	}
	
//	@Override
//	public boolean onTouch(View v, MotionEvent me) {
//		// TODO Auto-generated method stub
//
//// Phase 1
////		x = me.getX();
////		y = me.getY();
//
//// Phase 5
//		try {
//			Thread.sleep(50);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//// Phase 2		
//		switch (me.getAction()) {
//		case MotionEvent.ACTION_DOWN:
//			x = me.getX();
//			y = me.getY();
//			break;
//			
//		case MotionEvent.ACTION_UP:
//// Phase 3
////			x = me.getX();
////			y = me.getY();
//			break;
//			
//		case MotionEvent.ACTION_MOVE:
//// Phase 4
//			x = me.getX();
//			y = me.getY();
//			break;
//		
//		}
		

// Phase A
//		try {
//			Thread.sleep(50);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		switch (me.getAction() & MotionEvent.ACTION_MASK) {
//		case MotionEvent.ACTION_DOWN:
//			touchState = TouchType.DRAG;
//			x = me.getX();
//			y = me.getY();
//			break;
//					
//		case MotionEvent.ACTION_UP:
//			touchState = TouchType.NONE;
//			x = me.getX();
//			y = me.getY();
//			break;
//					
//		case MotionEvent.ACTION_MOVE:
//			if ( touchState == TouchType.DRAG ) {
//				x = me.getX();
//				y = me.getY();				
//			}
//			break;
//			
//		case MotionEvent.ACTION_POINTER_DOWN:
//			
//			x = me.getX();
//			y = me.getY();
//			break;
//					
//		case MotionEvent.ACTION_POINTER_UP:
//	        // Extract the index of the pointer that left the touch sensor
//	        final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
//	        final int pointerId = ev.getPointerId(pointerIndex);
//	        if (pointerId == mActivePointerId) {
//	            // This was our active pointer going up. Choose a new
//	            // active pointer and adjust accordingly.
//	            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
//	            mLastTouchX = ev.getX(newPointerIndex);
//	            mLastTouchY = ev.getY(newPointerIndex);
//	            mActivePointerId = ev.getPointerId(newPointerIndex);
//	        }
//			
//			x = me.getX();
//			y = me.getY();
//			break;
//									
//		}		
//		
//		return true;
//	}
	
    final static float MIN_DIST = 50;
    static float eventDistance = 0;
    static PointF center = new PointF( 0.0f, 0.0f );

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    	int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            // primary touch event starts: remember touch down location
            touchState = TouchType.DRAG;
            x = event.getX(0);
            y = event.getY(0);
            center.set(event.getX(0), event.getY(0));
            break;
            
        case MotionEvent.ACTION_POINTER_DOWN:
            // secondary touch event starts: remember distance and center
            eventDistance = calcDistance(event);
            center = calcMidpoint(event);
            if (eventDistance > MIN_DIST) {
                touchState = TouchType.ZOOM;
                scale = 1.0f;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (touchState == TouchType.DRAG) {
                // single finger drag, translate accordingly
                x = event.getX(0);
                y = event.getY(0);

            } else if (touchState == TouchType.ZOOM) {
                // double-finger zoom, scale accordingly around center
                float dist = calcDistance(event);
                if (dist > MIN_DIST) {
                    scale = dist / eventDistance;
                    x = center.x;
                    y = center.y;
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            touchState = TouchType.NONE;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            touchState = TouchType.DRAG;
            // update touch down location for drag event to holding finger
            final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
            switch (pointerIndex) {
            case 0:
                center = new PointF( event.getX(1), event.getY(1) );
                break;
            case 1:
                center = new PointF( event.getX(0), event.getY(0) );
                break;
            }
            break;
        }
        return true;
    }

    private float calcDistance(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    private PointF calcMidpoint(MotionEvent event) {
        return new PointF( (event.getX(0) + event.getX(1)) / 2, (event.getY(0) + event.getY(1)) / 2);
    }
	
	
	
	
	

	public class OurView extends SurfaceView implements Runnable {
    	
		Thread t = null;
		SurfaceHolder sh;
		boolean isThreadRunning = false;
		
    	public OurView(Context context) {
    		super(context);
    		sh = getHolder();
		}
    	
    	public void run() {
    		Rect nullRect = null;
    		Paint nullPaint = null;
    		
    		while (isThreadRunning) {
    			//perform some drawing
    			if (!sh.getSurface().isValid()) {
    				continue; //next loop
    			}
    			Canvas c = sh.lockCanvas(); //now we can paint to the canvas
    			c.drawARGB(255, 150, 150, 10);
//    			c.drawBitmap(icon, x-(icon.getWidth()/2), y-(icon.getHeight()/2), null);
    			c.drawBitmap(icon, nullRect, new RectF(x-(icon.getWidth()/2)*scale, y-(icon.getHeight()/2)*scale, 
    												   x+(icon.getWidth()/2)*scale, y+(icon.getHeight()/2)*scale),
    						 nullPaint);
       			Log.d(getLocalClassName() + "x,y,scale: ", Float.toString( x ) + " " + Float.toString( y ) + " " + Float.toString( scale ));
    			Log.d(getLocalClassName() + "bitmap Params: ", Float.toString( x-(icon.getWidth()/2)*scale ) + " " + Float.toString( y-(icon.getHeight()/2)*scale ) + " " +
    									   Float.toString( x+(icon.getWidth()/2)*scale ) + " " + Float.toString( y+(icon.getHeight()/2)*scale ));
    			
    			sh.unlockCanvasAndPost(c);
    				
    		}
    	};
    	
    	public void pause() {
    		isThreadRunning = false;
    		while (true){
    			try {
    				t.join();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			break;
    		}
    	};
    	
    	public void resume() {
    		isThreadRunning = true;
    		t = new Thread(this);
    		t.start();
    	};
    }


}